package com.ruoyi.production.service.impl.graftingclip;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.graftingclip.GraftingclipAmount;
import com.ruoyi.production.mapper.graftingclip.GraftingclipAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.graftingclip.GraftingclipPurchaseMapper;
import com.ruoyi.production.domain.graftingclip.GraftingclipPurchase;
import com.ruoyi.production.service.graftingclip.IGraftingclipPurchaseService;

/**
 * 嫁接夹购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class GraftingclipPurchaseServiceImpl implements IGraftingclipPurchaseService {
    @Autowired
    private GraftingclipPurchaseMapper graftingclipPurchaseMapper;
    @Autowired
    private GraftingclipAmountMapper graftingclipAmountMapper;

    /**
     * 查询嫁接夹购买信息
     *
     * @param graftingclipPurchaseId 嫁接夹购买信息ID
     * @return 嫁接夹购买信息
     */
    @Override
    public GraftingclipPurchase selectGraftingclipPurchaseById(Long graftingclipPurchaseId) {
        return graftingclipPurchaseMapper.selectGraftingclipPurchaseById(graftingclipPurchaseId);
    }

    /**
     * 查询嫁接夹购买信息列表
     *
     * @param graftingclipPurchase 嫁接夹购买信息
     * @return 嫁接夹购买信息
     */
    @Override
    public List<GraftingclipPurchase> selectGraftingclipPurchaseList(GraftingclipPurchase graftingclipPurchase) {
        return graftingclipPurchaseMapper.selectGraftingclipPurchaseList(graftingclipPurchase);
    }

    /**
     * 新增嫁接夹购买信息
     *
     * @param graftingclipPurchase 嫁接夹购买信息
     * @return 结果
     */
    @Override
    public int insertGraftingclipPurchase(GraftingclipPurchase graftingclipPurchase) {
        return graftingclipPurchaseMapper.insertGraftingclipPurchase(graftingclipPurchase);
    }

    /**
     * 修改嫁接夹购买信息
     *
     * @param graftingclipPurchase 嫁接夹购买信息
     * @return 结果
     */
    @Override
    public int updateGraftingclipPurchase(GraftingclipPurchase graftingclipPurchase) {
        Long graftingclipPurchaseId = graftingclipPurchase.getGraftingclipPurchaseId();
        //数据库存储的数据
        GraftingclipPurchase PurchaseDB = selectGraftingclipPurchaseById(graftingclipPurchaseId);
        //数据库存储的原数据的总量
        GraftingclipAmount originAmounts = new GraftingclipAmount();
        originAmounts.setGraftingclipSpecification(PurchaseDB.getGraftingclipSpecification());
        originAmounts.setOriginPlace(PurchaseDB.getOriginPlace());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setGraftingclipTypeId(PurchaseDB.getGraftingclipTypeId());
        graftingclipPurchase.setUpdateTime(DateUtils.getNowDate());
        List<GraftingclipAmount> amounts = graftingclipAmountMapper.selectGraftingclipAmountList(originAmounts);
        GraftingclipAmount originAmount = amounts.get(0);
        if (PurchaseDB.getGraftingclipTypeId().equals(graftingclipPurchase.getGraftingclipTypeId()) && PurchaseDB.getGraftingclipSpecification().equals(graftingclipPurchase.getGraftingclipSpecification()) && PurchaseDB.getOriginCompany().equals(graftingclipPurchase.getOriginCompany()) && PurchaseDB.getOriginPlace().equals(graftingclipPurchase.getOriginPlace())) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+graftingclipPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount() );
            graftingclipAmountMapper.updateGraftingclipAmount(originAmount);
        } else {
            GraftingclipAmount newAmounts = new GraftingclipAmount();
            newAmounts.setOriginPlace(graftingclipPurchase.getOriginPlace());
            newAmounts.setOriginCompany(graftingclipPurchase.getOriginCompany());
            newAmounts.setGraftingclipSpecification(graftingclipPurchase.getGraftingclipSpecification());
            newAmounts.setGraftingclipTypeId(graftingclipPurchase.getGraftingclipTypeId());
            List<GraftingclipAmount> amounts1 = graftingclipAmountMapper.selectGraftingclipAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)) {
                newAmounts.setStorageAmount(graftingclipPurchase.getPurchaseAmount());
                graftingclipAmountMapper.insertGraftingclipAmount(newAmounts);
            } else {
                GraftingclipAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount() + graftingclipPurchase.getPurchaseAmount());
                graftingclipAmountMapper.updateGraftingclipAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            graftingclipAmountMapper.updateGraftingclipAmount(originAmount);
        }

        return graftingclipPurchaseMapper.updateGraftingclipPurchase(graftingclipPurchase);
    }

    /**
     * 批量删除嫁接夹购买信息
     *
     * @param graftingclipPurchaseIds 需要删除的嫁接夹购买信息ID
     * @return 结果
     */
    @Override
    public int deleteGraftingclipPurchaseByIds(Long[] graftingclipPurchaseIds) {
        for (Long PurchaseId : graftingclipPurchaseIds) {
            GraftingclipPurchase graftingclipPurchase = selectGraftingclipPurchaseById(PurchaseId);
            GraftingclipAmount graftingclipAmount = new GraftingclipAmount();
            graftingclipAmount.setGraftingclipTypeId(graftingclipPurchase.getGraftingclipTypeId());
            graftingclipAmount.setGraftingclipSpecification(graftingclipPurchase.getGraftingclipSpecification());
            graftingclipAmount.setOriginCompany(graftingclipPurchase.getOriginCompany());
            graftingclipAmount.setOriginPlace(graftingclipPurchase.getOriginPlace());
            List<GraftingclipAmount> amounts = graftingclipAmountMapper.selectGraftingclipAmountList(graftingclipAmount);
            GraftingclipAmount graftingclipAmount1 = amounts.get(0);
            graftingclipAmount1.setStorageAmount(graftingclipAmount1.getStorageAmount() - graftingclipPurchase.getPurchaseAmount());
            graftingclipAmountMapper.updateGraftingclipAmount(graftingclipAmount1);
        }
        return graftingclipPurchaseMapper.deleteGraftingclipPurchaseByIds(graftingclipPurchaseIds);
    }

    /**
     * 删除嫁接夹购买信息信息
     *
     * @param graftingclipPurchaseId 嫁接夹购买信息ID
     * @return 结果
     */
    @Override
    public int deleteGraftingclipPurchaseById(Long graftingclipPurchaseId) {
        GraftingclipPurchase graftingclipPurchase = selectGraftingclipPurchaseById(graftingclipPurchaseId);
        GraftingclipAmount graftingclipAmount = new GraftingclipAmount();
        graftingclipAmount.setGraftingclipTypeId(graftingclipPurchase.getGraftingclipTypeId());
        graftingclipAmount.setGraftingclipSpecification(graftingclipPurchase.getGraftingclipSpecification());
        graftingclipAmount.setOriginCompany(graftingclipPurchase.getOriginCompany());
        graftingclipAmount.setOriginPlace(graftingclipPurchase.getOriginPlace());
        List<GraftingclipAmount> amounts = graftingclipAmountMapper.selectGraftingclipAmountList(graftingclipAmount);
        GraftingclipAmount graftingclipAmount1 = amounts.get(0);
        graftingclipAmount1.setStorageAmount(graftingclipAmount1.getStorageAmount() - graftingclipPurchase.getPurchaseAmount());
        graftingclipAmountMapper.updateGraftingclipAmount(graftingclipAmount1);
        return graftingclipPurchaseMapper.deleteGraftingclipPurchaseById(graftingclipPurchaseId);
    }

    /**
     * 插入嫁接夹购买表更新嫁接夹量表
     *
     * @param graftingclipPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(GraftingclipPurchase graftingclipPurchase) {
        GraftingclipAmount graftingclipAmount = new GraftingclipAmount();
        graftingclipAmount.setCreateTime(graftingclipPurchase.getCreateTime());
        graftingclipAmount.setCreateBy(graftingclipPurchase.getCreateBy());
        graftingclipAmount.setUpdateTime(graftingclipPurchase.getUpdateTime());
        graftingclipAmount.setUpdateBy(graftingclipPurchase.getUpdateBy());
        graftingclipAmount.setGraftingclipTypeId(graftingclipPurchase.getGraftingclipTypeId());
        graftingclipAmount.setGraftingclipSpecification(graftingclipPurchase.getGraftingclipSpecification());
        graftingclipAmount.setOriginCompany(graftingclipPurchase.getOriginCompany());
        graftingclipAmount.setOriginPlace(graftingclipPurchase.getOriginPlace());
        List<GraftingclipAmount> graftingclipAmounts = graftingclipAmountMapper.selectGraftingclipAmountList(graftingclipAmount);
        graftingclipAmount.setStorageAmount(graftingclipPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(graftingclipAmounts)) {
            GraftingclipAmount amount = graftingclipAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + graftingclipPurchase.getPurchaseAmount());
            graftingclipAmountMapper.updateGraftingclipAmount(amount);
        } else {
            graftingclipAmountMapper.insertGraftingclipAmount(graftingclipAmount);
        }
        return insertGraftingclipPurchase(graftingclipPurchase);
    }
}
